//Map Reader C++ File
//Reads data from a file to build a map
//Blue Laser Games		May 4, 2011

	//Windows Include Files

	//SDK Include Files

	//Project Include Files
#include "MapReader.h"
#include "Globals.h"
#include "BaseDrawable.h"

/*******************************************************\
*	Default Constructor: Default execution of init		*
\*******************************************************/
MapReader::MapReader()
{

}	//End MapReader()

/*******************************************************\
*	Destructor:  Default execution of release			*
\*******************************************************/
MapReader::~MapReader()
{

}	//End ~MapReader()

/*******************************************************\
*	ReadMapFromBitmap:  reads a bitmap into array		*   //PSEUDOCODE//
\*******************************************************/
//void MapReader::ReadMapFromBitmap(LPCSTR FileName, std::vector<int>& ObjMap)
//{
	//std::vector<color>			MapKey;
	//color						TempObj;

	////Load Image

	////Set MapKey
	//for(int i = 0; i < image.width; ++i)
	//{
	//	MapKey.push_back(image.getpixel(i,0));
	//}	//End for(top row)
	///*************************Set Object Map******************************\
	//*	This looks nastier than it really is.  All its doing is				*
	//*	iterating through every pixel in the bitmap and checking if			*
	//*	the pixel is equal to the MapKey, if so push_back its index			*
	//\*********************************************************************/
	//for(int y = 1; y < image.height; ++y)
	//{
	//	for(int x = 0; x < image.width; ++x)
	//	{
	//		for(int i = 0; i < MapKey.size(); ++i)
	//		{
	//			if(image.getpixel(x,y) == MapKey.at(i))
	//			{
	//				ObjMap.push_back(i);
	//			}	//End if(pixel == key)
	//		}	//End for(MapKey.size)
	//	}	//End for(width)
	//}	//End for(height)
	
//}	//End ReadMapFromBitmap()

/*******************************************************\
*	ReadMapFromText:  reads a text file into array		*
\*******************************************************/
bool MapReader::ReadMapFromText(LPCSTR FileName)
{
	std::ifstream				InFile;							//FileStream Object
	std::string					TempObj =		"NULL";			//Temp Object for loading strings to vector
	std::vector<TileData>		NewRow;							//Vector for creating new rows
	TileData					NewTile;						//Temp tile for loading into vector
	int	x = 0, y = 0;

		//Open File and Validate//
	InFile.open(FileName);
	if(!InFile.is_open())
		{::MessageBoxA(NULL, "File did not Open", "Unknown File", MB_OK);return false;}
	else
	{		
		////////////////////Read Object////////////////////
		InFile >> TempObj;			//Prime Input
		while(!InFile.eof() && TempObj != "#")
		{
			while(!InFile.eof() && TempObj != ";")
			{
				if(TempObj == "Sphinx")
					NewTile.Object = D3OBJ_SPHINX;
				if(TempObj == "Pharaoh")
					NewTile.Object = D3OBJ_PHARAOH;
				if(TempObj == "Anubis")
					NewTile.Object = D3OBJ_ANUBIS;
				if(TempObj == "Pyramid")
					NewTile.Object = D3OBJ_PYRAMID;
				if(TempObj == "Scarab")
					NewTile.Object = D3OBJ_SCARAB;
				if(TempObj == "Empty" || NewTile.Object == NULL)
					NewTile.Object = D3OBJ_EMPTY;

				NewRow.push_back(NewTile);
				NewTile.Object = NULL;
				InFile >> TempObj;
			}
			MapData.push_back(NewRow);
			NewRow.clear();
			InFile >> TempObj;
		}	//End while(!eof)
		TempObj = "NULL";

		////////////////////Read Owner////////////////////
		InFile >> TempObj;			//Prime Input
		while(!InFile.eof() && TempObj != "#")
		{
			while(!InFile.eof() && TempObj != ";")
			{
				if(TempObj == "Player1")
					MapData.at(y).at(x).Owner = Player1;
				if(TempObj == "Player2")
					MapData.at(y).at(x).Owner = Player2;
				if(TempObj == "Neutral" || MapData.at(y).at(x).Owner == NULL)
					MapData.at(y).at(x).Owner = Neutral;

				InFile >> TempObj;
				x++;
			}
			InFile >> TempObj;
			y++;
			x = 0;
		}	//End while(!eof)
		TempObj = "NULL";
		x = y = 0;

		////////////////////Read Direction////////////////////
		InFile >> TempObj;			//Prime Input
		while(!InFile.eof() && TempObj != "#")
		{
			while(!InFile.eof() && TempObj != ";")
			{
				if(TempObj == "West")
					MapData.at(y).at(x).Direction = DIR_WEST;
				if(TempObj == "East")
					MapData.at(y).at(x).Direction = DIR_EAST;
				if(TempObj == "South")
					MapData.at(y).at(x).Direction = DIR_SOUTH;
				if(TempObj == "North" || MapData.at(y).at(x).Direction == NULL)
					MapData.at(y).at(x).Direction = DIR_NORTH;

				InFile >> TempObj;
				x++;
			}
			InFile >> TempObj;
			y++;
			x = 0;
		}	//End while(!eof)
		TempObj = "NULL";
		x = y = 0;

		////////////////////Read Passable////////////////////
		InFile >> TempObj;			//Prime Input
		while(!InFile.eof() && TempObj != "#")
		{
			while(!InFile.eof() && TempObj != ";")
			{
				if(TempObj == "Player1")
					MapData.at(y).at(x).Passable = Player1;
				if(TempObj == "Player2")
					MapData.at(y).at(x).Passable = Player2;
				if(TempObj == "Neutral" || MapData.at(y).at(x).Passable == NULL)
					MapData.at(y).at(x).Passable = Neutral;

				InFile >> TempObj;
				x++;
			}
			InFile >> TempObj;
			y++;
			x = 0;
		}	//End while(!eof)
		TempObj = "NULL";
		x = y = 0;

	}	//End else(File open)

		//Close File and Validate//
	InFile.close();
	if(InFile.is_open())
		{::MessageBoxA(NULL, "File did not Close", FileName, MB_OK);return false;}
	
	return true;
}	//End ReadMapFromText()

bool MapReader::MoveObject(int x, int y, int Direction)
{
	TileData Temp = MapData.at(y).at(x);
	switch(Direction)
	{
	case DIR_NORTH:		//Space above is not limited to other player, out of bounds, or occupied
		if((GetMapData(x,y-1).Passable == Neutral || GetMapData(x,y-1).Passable == Temp.Owner) 
			&& (y-1) >= 0 && GetMapData(x,y-1).Object == D3OBJ_EMPTY)
		{
			SetMapData(x,y-1)->Direction	= Temp.Direction;
			SetMapData(x,y-1)->Object		= Temp.Object;
			SetMapData(x,y-1)->Owner		= Temp.Owner;
			SetMapData(x,y)->Direction	= DIR_NORTH;
			SetMapData(x,y)->Object		= D3OBJ_EMPTY;
			SetMapData(x,y)->Owner		= Neutral;
			return true;
		}
		break;
	case DIR_EAST:		//Space to right is not limited to other player, out of bounds, or occupied
		if((GetMapData(x+1,y).Passable == Neutral || GetMapData(x+1,y).Passable == Temp.Owner) 
			&& (x+1) < GetWidth() && GetMapData(x+1,y).Object == D3OBJ_EMPTY)
		{
			SetMapData(x+1,y)->Direction	= Temp.Direction;
			SetMapData(x+1,y)->Object		= Temp.Object;
			SetMapData(x+1,y)->Owner		= Temp.Owner;
			SetMapData(x,y)->Direction	= DIR_NORTH;
			SetMapData(x,y)->Object		= D3OBJ_EMPTY;
			SetMapData(x,y)->Owner		= Neutral;
			return true;
		}
		break;
	case DIR_SOUTH:		//Space below is not limited to other player, out of bounds, or occupied
		if((GetMapData(x,y+1).Passable == Neutral || GetMapData(x,y+1).Passable == Temp.Owner) 
			&& (y+1) < GetHeight() && GetMapData(x,y+1).Object == D3OBJ_EMPTY)
		{
			SetMapData(x,y+1)->Direction	= Temp.Direction;
			SetMapData(x,y+1)->Object		= Temp.Object;
			SetMapData(x,y+1)->Owner		= Temp.Owner;
			SetMapData(x,y)->Direction	= DIR_NORTH;
			SetMapData(x,y)->Object		= D3OBJ_EMPTY;
			SetMapData(x,y)->Owner		= Neutral;
			return true;
		}
		break;
	case DIR_WEST:		//Space to left is not limited to other player, out of bounds, or occupied
		if((GetMapData(x-1,y).Passable == Neutral || GetMapData(x-1,y).Passable == Temp.Owner) 
			&& (x-1) >= 0 && GetMapData(x-1,y).Object == D3OBJ_EMPTY)
		{
			SetMapData(x-1,y)->Direction	= Temp.Direction;
			SetMapData(x-1,y)->Object		= Temp.Object;
			SetMapData(x-1,y)->Owner		= Temp.Owner;
			SetMapData(x,y)->Direction		= DIR_NORTH;
			SetMapData(x,y)->Object			= D3OBJ_EMPTY;
			SetMapData(x,y)->Owner			= Neutral;
			return true;
		}
		break;
	}
	return false;
}

void MapReader::RotateObject(int x, int y, int EastWest)
{
	switch(EastWest)
	{
	case DIR_EAST:		
		SetMapData(x,y)->Direction++;
		if(GetMapData(x,y).Direction >= DIR_SIZE)
			SetMapData(x,y)->Direction = DIR_NORTH;
		break;
	case DIR_WEST:
		SetMapData(x,y)->Direction--;
		if(GetMapData(x,y).Direction < 0)
			SetMapData(x,y)->Direction = DIR_WEST;
		break;
	}
}

int MapReader::GetWidth()
{
	return MapData.at(0).size();
}

int MapReader::GetHeight()
{
	return MapData.size();
}

int MapReader::GetArea()
{
	return GetHeight() * GetWidth();
}

TileData* MapReader::SetMapData(int x, int y)
{
	if(x >= 0 && x < GetWidth() && y >= 0 && y < GetHeight())
	{
		return &MapData.at(y).at(x);
	}
	else
	{
		TileData Temp;
		Temp.Direction = DIR_NORTH;
		Temp.Object = D3OBJ_EMPTY;
		Temp.Owner = Neutral;
		Temp.Passable = false;
		return &Temp;
	}
}

TileData MapReader::GetMapData(int x, int y)
{
	if(x >= 0 && x < GetWidth() && y >= 0 && y < GetHeight())
	{
		return MapData.at(y).at(x);
	}
	else
	{
		TileData Temp;
		Temp.Direction = DIR_NORTH;
		Temp.Object = D3OBJ_BOUNDARY;
		Temp.Owner = Neutral;
		Temp.Passable = false;
		return Temp;
	}
}

void MapReader::ClearMap()
{
	for(unsigned int y = 0; y < MapData.size(); ++y)
	{
		MapData[y].clear();
	}
	MapData.clear();
}